# import tesserocr  
from PIL import Image  
# import pyautogui
import numpy as np 
import cv2  
import time
from datetime import datetime
import pyautogui  


def match_photo(screenshot,template):
    # ���ͼƬ�Ƿ�ɹ�����  
    if screenshot is None or template is None:  
        print("Error loading images!")  
        return None
        
    res = cv2.matchTemplate(screenshot, template, cv2.TM_CCOEFF_NORMED)  
    threshold = 0.85  # ƥ����ֵ  
    loc = np.where(res >= threshold)  
    
    # ���ڿ����ж��ƥ��㣬����ֻȡƥ�����ߵĵ�  
    if len(loc[0]) > 0:  
        # ע�⣺loc[0] �� loc[1] �ֱ���� x �� y ����  
        # ������Ҫ�ҵ� res �����е����ֵ���������������Ǽ򵥵�ȡ��һ��ƥ���  
        # ���Ҫ�ҵ����ƥ�䣬Ӧ��ʹ�� np.unravel_index(np.argmax(res), res.shape)  
        top_left = loc[1][0], loc[0][0]  # ע�� PIL �� OpenCV �� y,x ˳��ͬ  
        bottom_right = (top_left[0] + template.shape[1], top_left[1] + template.shape[0])  
    
        # ע�⣺���ǲ���ֱ���� screenshot_gray �ϻ��ƾ��Σ���Ϊ���� NumPy ����  
        # �����ǿ��Դ�ӡƥ��λ�ã����߽���ת���� PIL ͼ�񲢻��ƾ���  
        # print(f"Match found at ({top_left[0]}, {top_left[1]})") 
        return top_left 
    else:  
        print("No match found.")
        return None

# def Identify_image(photo):
#     photo.convert('L')
#     # ����PyTessBaseAPIʵ��  
#     with tesserocr.PyTessBaseAPI() as api:  
#         # ����Tesseract��·����ͨ�����Ǳ���ģ���Ϊtesserocr���Զ��ҵ�����  
#         # api.Init(r'D:\Program Files\Tesseract-OCR\', 'eng', tesserocr.OEM_LSTM_ONLY)  
#         api.SetVariable("tessedit_pageseg_mode", "4")  # PSMֵΪ3 
#         # �����ַ�������  
#         api.SetVariable("tessedit_char_whitelist", "0123456789")  
    
#         # ����ͼ��  
#         api.SetImage(photo)  
    
#         # ��ȡOCR���  
#         text = api.GetUTF8Text()  
#         return text
        
            
def get_acerage_current():
    # ʹ��pyautogui������Ļ��ͼ  
    screenshot = pyautogui.screenshot(region=(0, 0, 1920, 1080))  # ���������Ļ�ֱ�����1920x1080  
    # screenshot.save("./test.png")
    screenshot = np.array(screenshot.convert('L'))  # ��PILͼ��ת��Ϊ�Ҷ�NumPy����

    # ��ȡģ��ͼ  
    template = cv2.imread('./zero_point.png', 0)  
    # template = cv2.imread('C:\\Users\\Administrator\\Desktop\\zero_point.png', 0)  
    
    coordinate = match_photo(screenshot,template)

    if coordinate is not None:
        left = int(coordinate[0])+125
        top = int(coordinate[1])+217
        width = 60  # ��������� 60 ����  
        height = 15  # ����߶��� 20 ����  

        # ��ͼƬ  
        # screenshot_mA = Image.open('C:\\Users\\Administrator\\Desktop\\2024-09-0409-23-56.png').convert('L')  # ת��Ϊ�Ҷ�ͼ  
        screenshot_mA = pyautogui.screenshot(region=(left, top, width, height))  # ���������Ļ�ֱ�����1920x1080 
        back_up = screenshot_mA
        
        text = Identify_image(screenshot_mA)
        # ��ӡ���  
        # print(text)
        if(text!=''):
            return int(text)/100
        else:
            file_name = "./"
            now = datetime.now()  
            file_name += now.strftime("%Y-%m-%d%H-%M-%S")
            file_name += ".png"
            back_up.save(file_name)
            return None
# while True:
#     value = get_acerage_current()
#     # if value is None:
#     #     break
#     print(value)
#     time.sleep(0.5)
#template = Image.open('C:\\Users\\Administrator\\Desktop\\test33.png')  
#Identify_image(template)

def move_click(x,y):
    # �ƶ���굽��Ļ���� (100, 100)  
    x = x+5
    y = y+5
    pyautogui.moveTo(x, y, duration=1)  # 1�����ƶ���ָ��λ�� 
    # �ڵ�ǰλ�ý�������������  
    pyautogui.click() 


# file_path = 'D:\\auto\\'
file_path = 'C:\\Users\\Administrator\\Desktop\\'

while True:
    
    screenshot = pyautogui.screenshot(region=(0, 0, 1920, 1080))  # ���������Ļ�ֱ�����1920x1080  
    # screenshot.save(file_path+"screenshot.png")
    screenshot = np.array(screenshot.convert('L'))  # ��PILͼ��ת��Ϊ�Ҷ�NumPy����
    move_click(370,640)
    pyautogui.scroll(-500)

    template = cv2.imread(file_path+"jixu.png", 0)
    coordinate = match_photo(screenshot,template)
    if coordinate is not None:
        move_click(coordinate[0],coordinate[1])
        time.sleep(1)
        
    template = cv2.imread(file_path+"chongshi.png", 0)
    coordinate = match_photo(screenshot,template)
    if coordinate is not None:
        move_click(coordinate[0],coordinate[1])
        time.sleep(1)
    
    template = cv2.imread(file_path+"chongxin.png", 0)
    coordinate = match_photo(screenshot,template)
    
    if coordinate is not None:
        template = cv2.imread(file_path+"jieshou.png", 0)
        coordinate = match_photo(screenshot,template)
        if coordinate is not None:
            move_click(coordinate[0]+20,coordinate[1])
            time.sleep(8)
        move_click(110,962)
        time.sleep(1)
        text_to_type = "继续注释相关文件"
        pyautogui.hotkey('ctrl', 'v')

        pyautogui.hotkey('enter') 
        
    time.sleep(10)    
    
    